package edu.sctu.zhuyingzhi.month201506.day20150628.theSecondTestV2.Util;

import com.alibaba.fastjson.JSONObject;
import edu.sctu.zhuyingzhi.month201506.day20150628.theSecondTestV2.Data.MainData;

import java.io.*;

/**
 * Created by user13 on 2015/6/28.
 */
public class JsonUtil {
    /**
     * 把json值付给class
     * @param jsonFile
     * @return
     * @throws IOException
     */
    public MainData jsonToClass(String jsonFile) throws IOException {
        File file = new File(jsonFile);


        FileInputStream inputStream = new FileInputStream(file);
        String str = WebUtil.steamToString(inputStream);

//            MainData.getMainDate()  = JSONObject.parseObject(str,MainData.class);
        MainData mainData = JSONObject.parseObject(str, MainData.class);
        return mainData;


    }

    /**
     * 把赋值了的json序列化到文件
     * @param mainData
     * @param filePath
     */
    public void SerializableJson(MainData mainData, String filePath) {

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
            outputStream.writeObject(mainData);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化读取json
     * @param filePath
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public MainData antiSerializableJson(String filePath) throws IOException, ClassNotFoundException {

        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);


        MainData mainData = (MainData) objectInputStream.readObject();
        return mainData;
    }

    /**
     *
     * @param mainData
     */

}
